    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SpringLayout;

import ch.zarzu.champions.builder.logic.Listener;
import ch.zarzu.util.AppPath;

public class Power extends PowerListItem implements MouseListener {
    private JButton button, image, lock;
    private String image_path, app_path, id;
    private Listener listener;
    private SpringLayout layout;
    private TitlePhrase name;

    //init
    public Power(String image, String id_string) {
        app_path = AppPath.get();
        listener = Listener.getInstance();
        
        layout = new SpringLayout();
        setLayout(layout);
        image_path = app_path + image;
        
        id = id_string;
        
        draw();
    }
    
    /**
     * draw all elements
     */
    private void draw() {
        ImageIcon icon = new ImageIcon(app_path + "images/misc/lock.png");
        lock = new JButton(icon);
        lock.setBorder(null);
        lock.setFocusable(false);
        lock.setContentAreaFilled(false);
        lock.addMouseListener(this);
        lock.setPreferredSize(new Dimension(icon.getIconWidth() + 2, icon.getIconHeight()));
        lock.setVisible(false);
        
        icon = new ImageIcon(app_path + "images/empty_a.png");
        button = new JButton(icon);
        button.setBorder(null);
        button.setFocusable(false);
        button.setContentAreaFilled(false);
        button.setPreferredSize(new Dimension(icon.getIconWidth() + 2, icon.getIconHeight() + 4));
        button.addMouseListener(this);

        icon = new ImageIcon(image_path + "a.jpg");
        image = new JButton(icon);
        
        image.setBorder(null);
        image.setFocusable(false);
        image.setContentAreaFilled(false);
        image.setPreferredSize(new Dimension(icon.getIconWidth() + 2, icon.getIconHeight() + 4));
        image.addMouseListener(this);
        
        name = new TitlePhrase("", 123);
        
        redraw();
    }
    
    private void redraw() {
        removeAll();
        

        add(lock);
        add(name);
        add(image);
        add(button);
        
        layout.putConstraint(SpringLayout.EAST, lock, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, name, 40, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, name, 5, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, image, 3, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, image, 3, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, this, 0, SpringLayout.EAST, button);
        layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, button);
    }
    
    public void mouseEntered(MouseEvent e) {
        listener.powerMouseOver(id);
    }
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1 && e.getSource() != lock) {
            listener.powerClick(id);
        } else if(e.getButton() == MouseEvent.BUTTON3 || e.getSource() == lock) {
            listener.powerLock(id);
        }
    }
    
    public String getId() {
        return id;
    }
    
    public void setName(String n) {
        name = new TitlePhrase(n, 123);
        redraw();
    }
    
    public void setUsed(boolean used) {
        if(used) {
            button.setIcon(new ImageIcon(app_path + "images/empty_b.png"));
            image.setIcon(new ImageIcon(image_path + "b.jpg"));
        } else {
            button.setIcon(new ImageIcon(app_path + "images/empty_a.png"));
            image.setIcon(new ImageIcon(image_path + "a.jpg"));
        }
    }
    
    //sets the visibility of the lock
    public void setLockVisible(boolean b) {
        lock.setVisible(b);
    }
}